package twelfth.字节流与字符流基本操作.字节流.InputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class 使用read通过循环读取 {
    public static void main(String[] args) throws IOException {
        // 用File类找到文件
        String path = "D:" + File.separator + "WorkSpaces" + File.separator +
                "JavaDevelopBase_start_329" + File.separator + "src" + File.separator +
                "twelfth" + File.separator + "OutputStreamDemo.txt";
        /**
         * 注意输入文件的路径 如果指定的路径不存在 则运行一定会异常
         */
        File f = new File(path);// 必须给出路径
        InputStream is = new FileInputStream(f);
        byte[] b = new byte[(int) f.length()];
        // 使用read通过循环读取
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) is.read();
        }
        is.close();
        System.out.println("内容为：" + new String(b));
    }
}
/*
D:\WorkSpaces\Java\jdk-21\bin\java.exe "-javaagent:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=53742:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\WorkSpaces\JavaDevelopBase_start_329\out\production\JavaDevelopBase_start_329 twelfth.字节流与字符流基本操作.字节流.InputStream.使用read通过循环读取
内容为：你好java
加油，少年！

Process finished with exit code 0
 */